<?php

class CronController extends CCronController
{

    public function actionDispatchList()
    {
		
		 Yii::import('application.extensions.phpmailer.JPhpMailer');
        $trackers = Tracker::model()->findAll();
        foreach($trackers as $tracker){
            $dispatch = Dispatchdetails::model()->findByPk($tracker->dispatchno);
            if(!empty($dispatch->lastestTech->status)&& ($tracker->status != $dispatch->lastestTech->status)){
                $body = $this->renderPartial(
                    'tracker/emailtracker_statuschange_email_tpl',
                    array(
                        'model'=>$dispatch,
                        'oldstatus'=>$tracker->status
                    ),
                    true
                );

                $mail = new JPhpMailer;
                $mail->SetFrom('service@qsibanking.com', 'Customer Connect');
                $mail->AddReplyTo("service@qsibanking.com", "Customer Connect");
                $address = $tracker->email;
               	  $emailArray = explode(',', $address); 
	foreach($emailArray as $toAddress){
				$mail->AddAddress($toAddress);
			}
                $mail->Subject = "QSI, INC CC - Dispatch No. {$tracker->dispatchno} status has changed from {$tracker->status}  to  {$dispatch->lastestTech->status}";

                $mail->Body = $body;
                $mail->isHtml(false);
                $tracker->status = $dispatch->lastestTech->status;
                if ($mail->send())

                    if($tracker->status=='Complete'){
                        $tracker->delete();
                    } else {
                        $tracker->save();
                    }

            }
        }
    }
    public function actionCheckContract(){
  Yii::import('application.extensions.phpmailer.JPhpMailer');
         $contracts30DaysPeriod = Yii::app()->db
                        ->createCommand("SELECT c.*,l.*,DATEDIFF(contractenddate,NOW( ))  AS days_until_end
FROM tbl_contracts c
INNER JOIN tbl_locations l ON l.locno = c.locno AND l.custno = c.custno
WHERE DATEDIFF(  `contractenddate` , NOW( ) ) >=0
AND DATEDIFF(  `contractenddate` , NOW( ) ) <=30
ORDER BY days_until_end ASC
                        ")
                        ->queryAll();
 
    
     
        $contracts60DaysPeriod = Yii::app()->db
                        ->createCommand("SELECT c.*,l.*,DATEDIFF(contractenddate,NOW( ))  AS days_until_end
FROM tbl_contracts c
INNER JOIN tbl_locations l ON l.locno = c.locno AND l.custno = c.custno
WHERE DATEDIFF(  `contractenddate` , NOW( ) ) >30
AND DATEDIFF(  `contractenddate` , NOW( ) ) <=60
ORDER BY days_until_end ASC
                        ")
                        ->queryAll();
        $contracts90DaysPeriod = Yii::app()->db
                        ->createCommand("SELECT c.*,l.*,DATEDIFF(contractenddate,NOW( ))  AS days_until_end
FROM tbl_contracts c
INNER JOIN tbl_locations l ON l.locno = c.locno AND l.custno = c.custno
WHERE DATEDIFF(  `contractenddate` , NOW( ) ) >60
AND DATEDIFF(  `contractenddate` , NOW( ) ) <=90
ORDER BY days_until_end ASC
                        ")
                        ->queryAll();
        $body = $this->renderPartial('checkContracts', array(
            'contracts30DaysPeriod' => $contracts30DaysPeriod,
            'contracts60DaysPeriod' => $contracts60DaysPeriod,
            'contracts90DaysPeriod' => $contracts90DaysPeriod,
        ),true);
        $mail = new JPhpMailer;
        $mail->SetFrom('no-reply@customerconnect.qsibanking.com', 'Customer Connect');
        $mail->AddReplyTo("no-reply@customerconnect.qsibanking.com", "Customer Connect");
        $address = 'michael.bonza@qsibanking.com'; 
       // $address = 'markitanm@gmail.com';
        $mail->AddAddress($address, "Michael Bonza");
        $mail->Subject = 'Contracts Status';

        $mail->Body = $body;
        $mail->isHtml(false);
        if ($mail->send()) {
            echo 'message sent';
           
        }
        
    }
}
